package net.hvacapp.cmvrfforandroid.ui.project;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.mirror.android.common.util.StringUtils;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.adapter.BaseAdapterHelper;
import net.hvacapp.cmvrfforandroid.adapter.QuickAdapter;
import net.hvacapp.cmvrfforandroid.http.BasicHttpClient;
import net.hvacapp.cmvrfforandroid.http.HttpCallbackHandle;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.manager.ProjectManager;
import net.hvacapp.cmvrfforandroid.model.project.Project;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.MainTabBarActivity;
import net.hvacapp.cmvrfforandroid.ui.project.model.GetProductDataResp;
import net.hvacapp.cmvrfforandroid.ui.project.model.GetProjectTempleteDataResp;
import net.hvacapp.cmvrfforandroid.ui.project.model.GuidData;
import net.hvacapp.cmvrfforandroid.ui.project.model.ProjectInfoData;
import net.hvacapp.cmvrfforandroid.ui.project.model.ProjectTempleteData;
import net.hvacapp.cmvrfforandroid.ui.selections.model.AreaData;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;
import net.hvacapp.cmvrfforandroid.widget.CellSeperateLineView;
import net.hvacapp.cmvrfforandroid.widget.GroupSelectionView;
import net.hvacapp.cmvrfforandroid.widget.ItemSelectionView;
import net.hvacapp.cmvrfforandroid.widget.ProjectActionView;
import net.hvacapp.cmvrfforandroid.widget.UserSelectionView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EActivity(R.layout.activity_project_step3)
public class ProjectStep3Activity extends BaseActivity {

    @ViewById(R.id.action_view)
    ProjectActionView mActionView;


    private Dialog mProgressDlg;


    @ViewById(R.id.group_swj)
    GroupSelectionView mSwjGroup;
    @ViewById(R.id.group_qxsykt)
    GroupSelectionView mQxsyktGroup;
    @ViewById(R.id.group_kqnrsj)
    GroupSelectionView mKqnrsjGroup;
    @ViewById(R.id.group_qrjhq)
    GroupSelectionView mQrjhqGroup;

    @ViewById(R.id.ll_swj_content)
    LinearLayout mSwjContentLl;
    @ViewById(R.id.ll_qxsykt_content)
    LinearLayout mQxsyktContentLl;
    @ViewById(R.id.ll_qrjhq_content)
    LinearLayout mQrjhqContentLl;
    @ViewById(R.id.ll_kqnrsj_content)
    LinearLayout mKqnrsjContentLl;


    //室外机(swj),轻型商用空调(qxsykt),空气能热水机(kqnrsj),全热交换器(qrjhq)
    private List<GuidData> mSwjData;
    private List<GuidData> mQxsyktData;
    private List<GuidData> mKqnrsjData;
    private List<GuidData> mQrjhqData;

    private List<GuidData> mSwjSelectedData;
    private List<GuidData> mQxsyktSelectedData;
    private List<GuidData> mKqnrsjSelectedData;
    private List<GuidData> mQrjhqSelectedData;

    private boolean mSwjEnable;
    private boolean mQxsyktEnable;
    private boolean mKqnrsjEnable;
    private boolean mQrjhqEnable;

    private int mSwjSettingStatus;
    private int mQxsyktSettingStatus;
    private int mKqnrsjSettingStatus;
    private int mQrjhqSettingStatus;

    private int loadFinishedCount;
    private boolean hasModified;



    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        initViews();
        initEvent();
    }

    @Override
    public void onResume() {
        super.onResume();
        hasModified = false;
        mSwjSettingStatus = 0;
        mQxsyktSettingStatus = 0;
        mQrjhqSettingStatus = 0;
        mKqnrsjSettingStatus = 0;
    }

    /***
     * 初始化视图
     */
    private void initViews() {
        mActionView.setActionInfo(ProjectManager.getInstance().getTotal(), ProjectManager.getInstance().getCurrent());
    }

    /**
     * 初始化
     */
    @Override
    public void initBoot() {
        setupToolbar(R.string.title_for_project, "系统配置", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(AnnotationUtil.get(MainTabBarActivity.class));
            }
        });
        mSwjGroup.setTitleString("室外机");
        mSwjGroup.setInfoString("关闭");
        mQxsyktGroup.setTitleString("轻型商用空调");
        mQxsyktGroup.setInfoString("关闭");
        mKqnrsjGroup.setTitleString("空气能热水机");
        mKqnrsjGroup.setInfoString("关闭");
        mQrjhqGroup.setTitleString("全热交换器");
        mQrjhqGroup.setInfoString("关闭");
    }


    /**
     * 初始化数据
     */
    @Override
    public void initData(Intent intent) {
        mSwjData = new ArrayList<>();
        mSwjSelectedData = new ArrayList<>();
        mQxsyktData = new ArrayList<>();
        mQxsyktSelectedData = new ArrayList<>();
        mQrjhqData = new ArrayList<>();
        mQrjhqSelectedData = new ArrayList<>();
        mKqnrsjData = new ArrayList<>();
        mKqnrsjSelectedData = new ArrayList<>();

        mProgressDlg = createProgressBarDialog(this, "数据加载中...");
        mProgressDlg.show();

        loadFinishedCount = 0;
        loadProductSeries(ProjectInfoData.MACHINE_TAG_FOR_SWJ);
        loadProductSeries(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT);
        loadProductSeries(ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ);
        loadProductSeries(ProjectInfoData.MACHINE_TAG_FOR_QRJHQ);
    }



    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }

    private void initEvent() {
        mActionView.setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //上一步
                ProjectManager.getInstance().goLastStep();
                finish();
            }
        });

        mActionView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //下一步
                if (!hasModified) {
                    goNextStep();
                    return;
                }
                boolean isValid = checkProjectSeriesSelection();
                if (isValid == false) {
                    showMessage("请选择项目系列！");
                    return;
                }
                setAllProductSeriesSelection();
            }
        });

        mSwjGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hasModified = true;
                setProjectSeriesEnable(ProjectInfoData.MACHINE_TAG_FOR_SWJ, !mSwjEnable);
            }
        });

        mQxsyktGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hasModified = true;
                setProjectSeriesEnable(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT, !mQxsyktEnable);
            }
        });

        mKqnrsjGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hasModified = true;
                setProjectSeriesEnable(ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ, !mKqnrsjEnable);
            }
        });

        mQrjhqGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hasModified = true;
                setProjectSeriesEnable(ProjectInfoData.MACHINE_TAG_FOR_QRJHQ, !mQrjhqEnable);
            }
        });
    }

    /**
     *  获得可选得产品系列列表
     *
     *  @param tag
     */
    private void loadProductSeries(final String tag) {
        Map<String, Object> param = new HashMap<>();
        param.put("tag", tag);
        param.put("ac", 1); //0 仅包括未上市  1 仅包括已上市 2 已上市未上市都包括

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_PRODUCT_SERIES, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetProductDataResp>() {
            @Override
            public void onSuccess(GetProductDataResp obj) {
                // 成功
                loadFinishedCount++;
                if (loadFinishedCount == 4) {
                    mProgressDlg.dismiss();
                }
                loadProductSeriesIsEnable(tag);
                if (obj != null && obj.getGetProductXlsResult() != null && obj.getGetProductXlsResult().size() > 0) {
                    if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_SWJ)) {
                        if (mSwjData == null) {
                            mSwjData = new ArrayList<>();
                        }
                        mSwjData.clear();
                        mSwjData.addAll(obj.getGetProductXlsResult());
                    } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QXSYKT)) {
                        if (mQxsyktData == null) {
                            mQxsyktData = new ArrayList<>();
                        }
                        mQxsyktData.clear();
                        mQxsyktData.addAll(obj.getGetProductXlsResult());
                    } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ)) {
                        if (mKqnrsjData == null) {
                            mKqnrsjData = new ArrayList<>();
                        }
                        mKqnrsjData.clear();
                        mKqnrsjData.addAll(obj.getGetProductXlsResult());
                    } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QRJHQ)) {
                        if (mQrjhqData == null) {
                            mQrjhqData = new ArrayList<>();
                        }
                        mQrjhqData.clear();
                        mQrjhqData.addAll(obj.getGetProductXlsResult());
                    }
                } else {
//                    showMessage("获取产品系列失败");
                }
            }

            @Override
            public void onFailure(String response) {
                loadFinishedCount++;
                if (loadFinishedCount == 4) {
                    mProgressDlg.dismiss();
                }
                showMessage("获取产品系列失败");
            }
        });
    }

    /**
     *  获取项目的整个系列是否可以选择
     *
     *  @param tag
     */
    private void loadProductSeriesIsEnable(final String tag) {
        Map<String, Object> param = new HashMap<>();
        param.put("tag", tag);
        param.put("pid", ProjectManager.getInstance().getProjectData().getGuidId());

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_PRODUCT_SERIES_ENABLE, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetProductDataResp>() {
            @Override
            public void onSuccess(GetProductDataResp obj) {
                // 成功
                loadProjectProductSeries(tag);
                if (obj != null && obj.getGetEnableXlResult() > 0) {
                    if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_SWJ)) {
                        mSwjEnable = true;
                        ProjectManager.getInstance().setProjectSeriesEnable(ProjectInfoData.MACHINE_TAG_FOR_SWJ, mSwjEnable);
                    } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QXSYKT)) {
                        mQxsyktEnable = true;
                        ProjectManager.getInstance().setProjectSeriesEnable(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT, mQxsyktEnable);
                    } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ)) {
                        mKqnrsjEnable = true;
                        ProjectManager.getInstance().setProjectSeriesEnable(ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ, mKqnrsjEnable);
                    } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QRJHQ)) {
                        mQrjhqEnable = true;
                        ProjectManager.getInstance().setProjectSeriesEnable(ProjectInfoData.MACHINE_TAG_FOR_QRJHQ, mQrjhqEnable);
                    }
                }
            }

            @Override
            public void onFailure(String response) {
                showMessage("获取产品系列设置失败");
            }
        });

    }


    /**
     *  获取项目中选择了的系列
     *
     *  @param tag
     */
    private void loadProjectProductSeries(final String tag) {
        Map<String, Object> param = new HashMap<>();
        param.put("tag", tag);
        param.put("pid", ProjectManager.getInstance().getProjectData().getGuidId());

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_PROJECT_PRODUCT_SERIES, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetProductDataResp>() {
            @Override
            public void onSuccess(GetProductDataResp obj) {
                // 成功
                if (obj != null && obj.getGetCandidateXlsResult() != null) {
                    if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_SWJ)) {
                        mSwjSelectedData.addAll(obj.getGetCandidateXlsResult());
                        setProjectSeriesSelected(mSwjData, mSwjSelectedData);
                        setupSeriesViews(tag);
                    } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QXSYKT)) {
                        mQxsyktSelectedData.addAll(obj.getGetCandidateXlsResult());
                        setProjectSeriesSelected(mQxsyktData, mQxsyktSelectedData);
                        setupSeriesViews(tag);
                    } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ)) {
                        mKqnrsjSelectedData.addAll(obj.getGetCandidateXlsResult());
                        setProjectSeriesSelected(mKqnrsjData, mKqnrsjSelectedData);
                        setupSeriesViews(tag);
                    } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QRJHQ)) {
                        mQrjhqSelectedData.addAll(obj.getGetCandidateXlsResult());
                        setProjectSeriesSelected(mQrjhqData, mQrjhqSelectedData);
                        setupSeriesViews(tag);
                    }
                } else {
                    setupSeriesViews(tag);
                }
            }

            @Override
            public void onFailure(String response) {
                showMessage("获取项目产品系列失败");
            }
        });
    }

    private void setProjectSeriesSelected(List<GuidData> originData, List<GuidData> selectedData) {
        for (GuidData item : originData) {
            for (GuidData sItem : selectedData) {
                if (StringUtils.isEquals(sItem.getGuidId(), item.getGuidId())) {
                    item.setSelected(true);
                    break;
                }
                else {
                    item.setSelected(false);
                }
            }
        }
    }

    private void setupSeriesViews(String tag) {
        if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_SWJ)) {
            mSwjGroup.setGroupVisible(mSwjEnable);
            if (mSwjEnable) {
                mSwjGroup.setInfoString("开启");
                mSwjContentLl.setVisibility(View.VISIBLE);
            }
            else {
                mSwjGroup.setInfoString("关闭");
                mSwjContentLl.setVisibility(View.GONE);
            }
            for (final GuidData item : mSwjData) {
                CellSeperateLineView line = new CellSeperateLineView(this);
                mSwjContentLl.addView(line);

                final ItemSelectionView view = new ItemSelectionView(this);
                view.setTitleString(item.getName());
                view.setItemSelected(item.isSelected());
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        hasModified = true;
                        item.setSelected(!item.isSelected());
                        view.setItemSelected(item.isSelected());
                        setDataSelected(mSwjSelectedData, item);

                    }
                });
                mSwjContentLl.addView(view);
            }

        } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QXSYKT)) {
            mQxsyktGroup.setGroupVisible(mQxsyktEnable);
            if (mQxsyktEnable) {
                mQxsyktGroup.setInfoString("开启");
                mQxsyktContentLl.setVisibility(View.VISIBLE);
            }
            else {
                mQxsyktGroup.setInfoString("关闭");
                mQxsyktContentLl.setVisibility(View.GONE);
            }
            for (final GuidData item : mQxsyktData) {
                CellSeperateLineView line = new CellSeperateLineView(this);
                mQxsyktContentLl.addView(line);

                final ItemSelectionView view = new ItemSelectionView(this);
                view.setTitleString(item.getName());
                view.setItemSelected(item.isSelected());
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        hasModified = true;
                        item.setSelected(!item.isSelected());
                        view.setItemSelected(item.isSelected());
                        setDataSelected(mQxsyktSelectedData, item);
                    }
                });
                mQxsyktContentLl.addView(view);
            }
        } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ)) {
            mKqnrsjGroup.setGroupVisible(mKqnrsjEnable);
            if (mKqnrsjEnable) {
                mKqnrsjGroup.setInfoString("开启");
                mKqnrsjContentLl.setVisibility(View.VISIBLE);
            }
            else {
                mKqnrsjGroup.setInfoString("关闭");
                mKqnrsjContentLl.setVisibility(View.GONE);
            }
            for (final GuidData item : mKqnrsjData) {
                CellSeperateLineView line = new CellSeperateLineView(this);
                mKqnrsjContentLl.addView(line);

                final ItemSelectionView view = new ItemSelectionView(this);
                view.setTitleString(item.getName());
                view.setItemSelected(item.isSelected());
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        hasModified = true;
                        item.setSelected(!item.isSelected());
                        view.setItemSelected(item.isSelected());
                        setDataSelected(mKqnrsjSelectedData, item);
                    }
                });
                mKqnrsjContentLl.addView(view);
            }
        } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QRJHQ)) {
            mQrjhqGroup.setGroupVisible(mQrjhqEnable);
            if (mQrjhqEnable) {
                mQrjhqGroup.setInfoString("开启");
                mQrjhqContentLl.setVisibility(View.VISIBLE);
            } else {
                mQrjhqGroup.setInfoString("关闭");
                mQrjhqContentLl.setVisibility(View.GONE);
            }
            for (final GuidData item : mQrjhqData) {
                CellSeperateLineView line = new CellSeperateLineView(this);
                mQrjhqContentLl.addView(line);

                final ItemSelectionView view = new ItemSelectionView(this);
                view.setTitleString(item.getName());
                view.setItemSelected(item.isSelected());
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        hasModified = true;
                        item.setSelected(!item.isSelected());
                        view.setItemSelected(item.isSelected());
                        setDataSelected(mQrjhqSelectedData, item);
                    }
                });
                mQrjhqContentLl.addView(view);
            }
        }

    }

    private void setDataSelected(List<GuidData> selectedData, GuidData data) {
        if (data.isSelected()) {
            boolean hasFind = false;
            for (GuidData item : selectedData) {
                if (StringUtils.isEquals(item.getGuidId(), data.getGuidId())) {
                    hasFind = true;
                    break;
                }
            }
            if (!hasFind) {
                selectedData.add(data);
            }
        }
        else {
            for (GuidData item : selectedData) {
                if (StringUtils.isEquals(item.getGuidId(), data.getGuidId())) {
                    selectedData.remove(item);
                    break;
                }
            }
        }
    }

   private void setProjectSeriesEnable(final String tag, boolean enable) {
       String url = HttpConstant.HTTP_POST_PRODUCT_SERIES_ENABLE_SETTING
               + "?tk=" + AccountManager.getInstance().getToken()
               + "&pid=" + ProjectManager.getInstance().getProjectData().getGuidId()
               + "&tag=" + tag
               + "&enable=" + (enable ? "1" : "0");

       mProgressDlg = createProgressBarDialog(this, "系列开关修改中...");
       mProgressDlg.show();

       BasicHttpClient.httpPostAsync(url,"", new HttpCallbackHandle<GetProductDataResp>() {
           @Override
           public void onSuccess(GetProductDataResp obj) {
               // 成功
                mProgressDlg.dismiss();
               if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_SWJ)) {
                   mSwjEnable = !mSwjEnable;
                   ProjectManager.getInstance().setProjectSeriesEnable(ProjectInfoData.MACHINE_TAG_FOR_SWJ, mSwjEnable);
                   mSwjGroup.setGroupVisible(mSwjEnable);
                   if (mSwjEnable) {
                       mSwjGroup.setInfoString("开启");
                       mSwjContentLl.setVisibility(View.VISIBLE);
                   } else {
                       mSwjGroup.setInfoString("关闭");
                       mSwjContentLl.setVisibility(View.GONE);
                   }
               } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QXSYKT)) {
                   mQxsyktEnable = !mQxsyktEnable;
                   ProjectManager.getInstance().setProjectSeriesEnable(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT, mQxsyktEnable);
                   mQxsyktGroup.setGroupVisible(mQxsyktEnable);
                   if (mQxsyktEnable) {
                       mQxsyktGroup.setInfoString("开启");
                       mQxsyktContentLl.setVisibility(View.VISIBLE);
                   }
                   else {
                       mQxsyktGroup.setInfoString("关闭");
                       mQxsyktContentLl.setVisibility(View.GONE);
                   }
               } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ)) {
                   mKqnrsjEnable = !mKqnrsjEnable;
                   ProjectManager.getInstance().setProjectSeriesEnable(ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ, mKqnrsjEnable);
                   mKqnrsjGroup.setGroupVisible(mKqnrsjEnable);
                   if (mKqnrsjEnable) {
                       mKqnrsjGroup.setInfoString("开启");
                       mKqnrsjContentLl.setVisibility(View.VISIBLE);
                   }
                   else {
                       mKqnrsjGroup.setInfoString("关闭");
                       mKqnrsjContentLl.setVisibility(View.GONE);
                   }
               } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QRJHQ)) {
                   mQrjhqEnable = !mQrjhqEnable;
                   ProjectManager.getInstance().setProjectSeriesEnable(ProjectInfoData.MACHINE_TAG_FOR_QRJHQ, mQrjhqEnable);
                   mQrjhqGroup.setGroupVisible(mQrjhqEnable);
                   if (mQrjhqEnable) {
                       mQrjhqGroup.setInfoString("开启");
                       mQrjhqContentLl.setVisibility(View.VISIBLE);
                   } else {
                       mQrjhqGroup.setInfoString("关闭");
                       mQrjhqContentLl.setVisibility(View.GONE);
                   }
               }
           }

           @Override
           public void onFailure(String response) {
               mProgressDlg.dismiss();
               showMessage("系列开关修改失败，请重试");
           }
       });
   }

    private boolean checkProjectSeriesSelection() {
        if (mSwjEnable && mSwjSelectedData.size() == 0) {
            return false;
        }
        if (mQxsyktEnable && mQxsyktSelectedData.size() == 0) {
            return false;
        }
        if (mQrjhqEnable && mQrjhqSelectedData.size() == 0) {
            return false;
        }
        if (mKqnrsjEnable && mKqnrsjSelectedData.size() == 0) {
            return false;
        }

        return true;
    }


    /**
     *  上传服务器用户选择的系列
     */
    private void setAllProductSeriesSelection() {
        mProgressDlg = createProgressBarDialog(this, "系列选择修改中...");
        mProgressDlg.show();

        if(mSwjSettingStatus == 0) {
            mSwjSettingStatus = 1;
            setProjectSeriesSelection(ProjectInfoData.MACHINE_TAG_FOR_SWJ);
        }
        if(mQxsyktSettingStatus == 0) {
            mQxsyktSettingStatus = 1;
            setProjectSeriesSelection(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT);
        }
        if(mQrjhqSettingStatus == 0) {
            mQrjhqSettingStatus = 1;
            setProjectSeriesSelection(ProjectInfoData.MACHINE_TAG_FOR_QRJHQ);
        }
        if(mKqnrsjSettingStatus == 0) {
            mKqnrsjSettingStatus = 1;
            setProjectSeriesSelection(ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ);
        }
    }

    /**
     *  上传用户选择的系列
     *
     *  @param tag      <#tag description#>
     */
    private void setProjectSeriesSelection(final String tag) {
        List<String> guidIds = new ArrayList<>();
        if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_SWJ)) {
            for (GuidData item : mSwjSelectedData) {
                guidIds.add(item.getGuidId());
            }
        } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QXSYKT)) {
            for (GuidData item : mQxsyktSelectedData) {
                guidIds.add(item.getGuidId());
            }
        } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ)) {
            for (GuidData item : mKqnrsjSelectedData) {
                guidIds.add(item.getGuidId());
            }
        } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QRJHQ)) {
            for (GuidData item : mQrjhqSelectedData) {
                guidIds.add(item.getGuidId());
            }
        }

        Map<String, Object> param = new HashMap<>();
        param.put("xl", guidIds);

        String url = HttpConstant.HTTP_POST_PROJECT_PRODUCT_SERIES
                + "?tk=" + AccountManager.getInstance().getToken()
                + "&pid=" + ProjectManager.getInstance().getProjectData().getGuidId()
                + "&tag=" + tag;

        BasicHttpClient.httpPostAsync(url, JSON.toJSONString(param), new HttpCallbackHandle<GetProductDataResp>() {
            @Override
            public void onSuccess(GetProductDataResp obj) {
                // 成功

                if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_SWJ)) {
                    if (mSwjSelectedData.size() > 0) {
                        ProjectManager.getInstance().setProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_SWJ, mSwjSelectedData.get(0).getGuidId());
                    }
                    else {
                        ProjectManager.getInstance().setProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_SWJ,"");
                    }
                    mSwjSettingStatus = 2;
                } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QXSYKT)) {
                    if (mQxsyktSelectedData.size() > 0) {
                        ProjectManager.getInstance().setProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT, mQxsyktSelectedData.get(0).getGuidId());
                    }
                    else {
                        ProjectManager.getInstance().setProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT,"");
                    }
                    mQxsyktSettingStatus = 2;
                } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ)) {
                    if (mKqnrsjSelectedData.size() > 0) {
                        ProjectManager.getInstance().setProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ, mKqnrsjSelectedData.get(0).getGuidId());
                    }
                    else {
                        ProjectManager.getInstance().setProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ,"");
                    }
                    mKqnrsjSettingStatus = 2;
                } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QRJHQ)) {
                    if (mQrjhqSelectedData.size() > 0) {
                        ProjectManager.getInstance().setProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_QRJHQ, mQrjhqSelectedData.get(0).getGuidId());
                    }
                    else {
                        ProjectManager.getInstance().setProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_QRJHQ,"");
                    }
                    mQrjhqSettingStatus = 2;
                }
                checkSettingSuccess();
            }

            @Override
            public void onFailure(String response) {
                if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_SWJ)) {
                    mSwjSettingStatus = 0;
                } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QXSYKT)) {
                    mQxsyktSettingStatus = 0;
                } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_KQNRSJ)) {
                    mKqnrsjSettingStatus = 0;
                } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QRJHQ)) {
                    mQrjhqSettingStatus = 0;
                }
                checkSettingSuccess();
            }
        });


    }

    /**
     *  检查是否所有的4项都上传成功，如果都成功，就跳转到下个页面，如果有一个失败，就等所有的上传完成后，给出失败提示
     */
    private void checkSettingSuccess() {
        if (mSwjSettingStatus == 1 || mQxsyktSettingStatus == 1 || mKqnrsjSettingStatus == 1 || mQrjhqSettingStatus == 1) {
            //还有项目在上传中
            return;
        }
        else if (mSwjSettingStatus == 2 && mQxsyktSettingStatus == 2 && mKqnrsjSettingStatus == 2 && mQrjhqSettingStatus == 2) {
            //上传成功
            mProgressDlg.dismiss();
            //跳转到下一页
            goNextStep();
        }
        else {
            mProgressDlg.dismiss();
            showMessage("上传选择系统失败，请重试！");
        }
    }

    private void goNextStep() {
        ProjectManager.getInstance().updateTotalSteps();
        ProjectManager.getInstance().goNextStep();
        if (ProjectManager.getInstance().roomPageEnabled()) {
            //跳转到房间页面
            startActivity(AnnotationUtil.get(ProjectStep4Activity.class));
        }
        else if (ProjectManager.getInstance().swjPageEnabled()) {
            //跳转到室外机页面
            startActivity(AnnotationUtil.get(ProjectStep5Activity.class));
        }
        else if (ProjectManager.getInstance().qrjhqPageEnabled()) {
            //跳转到全热交换器页面
            startActivity(AnnotationUtil.get(ProjectStep6Activity.class));
        }
        else if (ProjectManager.getInstance().kqnrsjPageEnabled()) {
            //跳转到空气能热水机页面
            startActivity(AnnotationUtil.get(ProjectStep7Activity.class));
        }
        else {
            //跳转到设备报价页面
            startActivity(AnnotationUtil.get(ProjectStep8Activity.class));
        }
    }


}
